package com.company.nuwa.common.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>校验结果</p>
 *
 * @author Lionel Lee
 * @version 1.0: ValidResult.java
 * @date 2020/5/21 13:52
 */

@Data
@Accessors(chain = true)
@AllArgsConstructor
public class ValidResult implements Serializable {

    private boolean            hasErrors;

    private List<ErrorMessage> errors;

    public ValidResult() {
        this.errors = new ArrayList<>();
    }

    public boolean hasErrors() {
        return hasErrors;
    }

    public void setHasErrors(boolean hasErrors) {
        this.hasErrors = hasErrors;
    }

    /**
     * 获取所有验证信息
     * @return 集合形式
     */
    public List<ErrorMessage> getAllErrors() {
        return errors;
    }

    /**
     * 获取所有验证信息
     * @return 字符串形式
     */
    public String getErrors() {
        StringBuilder sb = new StringBuilder();
        for (ErrorMessage error : errors) {
            sb.append(error.getPropertyPath()).append(":").append(error.getMessage()).append(" ");
        }
        return sb.toString();
    }

    public void addError(String propertyName, String message) {
        this.errors.add(new ErrorMessage(propertyName, message));
    }
}